package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataMapping;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnMappingRule;

public class DataMappingLoader extends NodeLoader {

	public List<Shape> loadNodes(Element root) {
		NodeList datamappingnodes = root
				.getElementsByTagName("saturndata-mapping");

		List<Shape> shapes = new ArrayList<Shape>(datamappingnodes.getLength());
		for (int i = 0; i < datamappingnodes.getLength(); ++i) {

			SaturnDataMapping saturnDataMapping = new SaturnDataMapping();
			Node datamappingnode = datamappingnodes.item(i);

			String shapeId = super.getNodeAttributeValue(datamappingnode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(datamappingnode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			Point location = super.getLocation(datamappingnode);
			Dimension size = new Dimension(50, 70);

			String name = super.getNodeElementValue(datamappingnode, "name");
			String description = super.getNodeElementValue(datamappingnode,
					"description");

			String valuekey = super.getNodeAttributeValue(getChildNode(datamappingnode, "target"),
					"value-key");
			String fullkey = super.getNodeAttributeValue(getChildNode(datamappingnode, "target"),
					"fullkey");

			saturnDataMapping.setId(shapeId);
			saturnDataMapping.setNextId(nextId);
			saturnDataMapping.setSize(size);
			saturnDataMapping.setLocation(location);
			saturnDataMapping.setName(name);
			saturnDataMapping.setValuekey(valuekey);
			saturnDataMapping.setDescription(description);
			saturnDataMapping.setFullkey(fullkey);
			// TODO：解析rules
			saturnDataMapping.setSaturnMappingRules(loadrules(saturnDataMapping, datamappingnode));
			saturnDataMapping.setNode(datamappingnode);
			shapes.add(saturnDataMapping);
		}

		return shapes;
	}

	private List<SaturnMappingRule> loadrules(SaturnDataMapping saturnDataMapping,
			Node datamappingnode) {
		Node mappingrules = this.getChildNode(datamappingnode, "mapping-rules");
		List<SaturnMappingRule> saturnMappingRulea = new ArrayList<SaturnMappingRule>();

		if (mappingrules != null) {
			List<Node> ruleNodes = this.getChildNodes(mappingrules, "rule");

			for (Node ruleNode : ruleNodes) {
				saturnMappingRulea.add(this.loadrule(ruleNode));
			}
		}
		return saturnMappingRulea;
	}

	private SaturnMappingRule loadrule(Node ruleNode) {
		SaturnMappingRule saturnMappingRule = new SaturnMappingRule();
		String key = super.getNodeAttributeValue(ruleNode, "key");
		String value = super.getNodeAttributeValue(ruleNode, "value");
		String source = super.getNodeAttributeValue(ruleNode, "source");
		String fullkey = super.getNodeAttributeValue(ruleNode, "fullkey");

		saturnMappingRule.setKey(key);
		saturnMappingRule.setValue(value);
		saturnMappingRule.setSource(source);
		saturnMappingRule.setFullkey(fullkey);
		return saturnMappingRule;
	}

}
